<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% content_for :page_title do %><%= join_title @context.name, @assignment.title, t(:page_title, "Peer Reviews") %><% end %>
<%- css_bundle(:assignments_peer_review) %>

<% content_for :right_side do %>
  <a href="<%= context_url(@context, :context_assignment_url, @assignment.id) %>" class='btn button-sidebar-wide'>
    {{> icons/arrow-left}}
    <%= t 'links.back_to_assignment', "Back to Assignment" %>
  </a>
  <p><%= t :details, "To the left you can see the list of students for this assignment, and also
  which student submissions (if any) they have been assigned to review." %></p>
  <% unless @submissions.empty? %>
    <%= form_tag context_url(@context, :context_assignment_assign_peer_reviews_url, @assignment.id), {} do %>
      <h3 style="margin-bottom: 0;"><%= t 'titles.automatically_assign_reviews', "Automatically Assign Reviews" %></h3>
      <p><%= t 'descriptions.automatically_assign_reviews', "You can assign peer reviews to those users who have submitted but don't already have
      reviews assigned by clicking the button below." %></p>
      <% if @assignment.group_category %>
        <div>
          <%= check_box_tag "intra_group_peer_reviews", "1", @assignment.intra_group_peer_reviews %>
          <label for="intra_group_peer_reviews">{{#t}}Allow intra-group peer reviews{{/t}}</label>
        </div>
      <% end %>
      <%= t :reviews_per_user, "%{count} reviews per user", :count => %{<input type="text" name="peer_review_count" value="#{@assignment.peer_review_count || 1}" style="width: 25px;"/>}.html_safe %>
      <div class='button-container button-container-right-aligned'>
        <button type="submit" class="btn btn-large"><%= t 'buttons.assign_peer_reviews', "Assign Peer Reviews" %></button>
      </div>
    <% end %>
  <% end %>
<% end %>
<% js_bundle 'legacy/assignments_peer_reviews' %>

<h1><%= t :title, "%{assignment} Peer Reviews", :assignment => @assignment.title %></h1>
  <% if @assignment.rubric_association %>
    <%= t :overview_with_rubric, "Student peer reviews will be considered complete when students have commented
      at least once on the page and filled out the rubric form for the assignment." %>
  <% else %>
    <%= t :overview, "Student peer reviews will be considered complete when students have commented
      at least once on the page." %>
  <% end %>
<ul class="unstyled_list" style="margin-left: 20px;">
  <li style="<%= hidden unless @students.empty? %>" class="no_students_message"><%= t 'messages.no_students_found', "No Students Found" %></li>
  <% @students.each do |student| %>
    <% submission = @submissions.to_a.find{|s| s.user_id == student.id} %>
    <li style="font-size: 1.2em; margin-bottom: 10px;" class="student_reviews">
      <a <%= submission && submission.has_submission? ? "class='icon-warning person' title='#{h(t(:submitted, "submitted"))}'" : "class='person_gray' title='#{h(t(:not_yet_submitted, "not yet submitted"))}'" %> href="<%= context_url(@context, :context_assignment_submission_url, @assignment.id, student.id) %>">
        <span class="assessor_name"><%= student.last_name_first %></span>
        <span class="user_id student_review_id" style="display: none;"><%= student.id %></span>
      </a>
      <ul class="unstyled_list peer_reviews" style="padding-left: 21px; font-size: 0.8em; width: 50%;">
        <li class="peer_review no_requests_message" style="<%= hidden unless !submission || submission.assigned_assessments.empty? %>"><%= t :none_assigned, "None Assigned" %></li>
        <% if submission %>
        <% Canvas::ICU.collate_by(submission.assigned_assessments) {|r| r.asset.user.sortable_name }.each do |request| %>
          <% if (request && request.asset && request.asset.user) %>
            {{> views/peer_review_assignment}}
          <% end %>
        <% end %>
        <% end %>
      </ul>
      <div style="margin-left: 25px; font-size: 0.7em;" class="assign_box">
        <a href="#" class="assign_peer_review_link"><%= t 'links.assign_peer_review', "Give %{student} another submission to assess", :student => student.name %>
          {{> icons/add}}
        </a>
        <div class="form_content">
        </div>
      </div>
    </li>
  <% end %>
</ul>
<%= form_tag ".", {:id => "assign_peer_review_form", :style => "display: none;"} do %>
  <select name="reviewee_id">
    <option value=""><%= t 'options.select_student', "[ Select Student ]" %></option>
    <% @students.each do |student| %>
      <option value="<%= student.id %>" class="student_<%= student.id %>"><%= student.last_name_first %></option>
    <% end %>
  </select>
  <button type="submit" class="Button Button--small"><%= t 'buttons.add', 'Add' %></button>
<% end %>
<a href="<%= context_url(@context, :context_assignment_assign_peer_review_url, @assignment.id, '{{ reviewer_id }}') %>" style="display: none;" class="assign_peer_review_url">&nbsp;</a>
<ul style="display: none;">
{{> views/peer_review_assignment}}
</ul>
